package com.jidong.test_springboot.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jidong.test_springboot.dao.DeptDao;
import com.jidong.test_springboot.entity.Dept;
import com.jidong.test_springboot.vo.ResultData;

import lombok.extern.slf4j.Slf4j;


@Controller
@RequestMapping("dept/") 
@Slf4j  
public class DeptServlet {

	@Autowired      
	private DeptDao dao;

	@RequestMapping("add.do")
	@ResponseBody
	@RequiresPermissions({"dept:add"})
	public ResultData add(Dept dept,HttpSession session,ResultData result) throws IOException, ServletException {
		
		int r = dao.insert(dept);
		if (r > 0) {
			return result.setCode(200).setMsg("添加成功");
		} else {
			return result.setCode(200).setMsg("添加失败");
		}
	}

	@RequestMapping("list.do")
	@ResponseBody
	@RequiresPermissions({"dept:list"})
	public ResultData list(HttpSession session ,ResultData result)  {
		List<Dept> depts = dao.selectList(null);

		return result.setCode(200).setObj(depts);
	}

	@RequestMapping("update.do")
	@ResponseBody
	@RequiresPermissions({"dept:update"})
	public ResultData update(Dept dept,HttpSession session, HttpServletResponse response) throws IOException, ServletException {
		ResultData result=new ResultData();
		int r=dao.updateById(dept);
		if(r>0) {
			result.setCode(200);
			result.setMsg("删除成功");
			return result;
		}else {
			result.setCode(200);
			result.setMsg("删除失败");
			return result;
		}
		
	}
	@RequestMapping("delete.do")
	@ResponseBody
	@RequiresPermissions({"dept:delete"})
	public ResultData delete(int id, HttpServletResponse response,HttpSession session) throws IOException, ServletException {
		ResultData result=new ResultData();
		
		int r=dao.deleteById(id);
		if(r>0) {
			result.setCode(200);
			result.setMsg("删除成功");
			return result;
		}else {
			result.setCode(200);
			result.setMsg("删除失败");
			return result;
		}
	}

}
